package com.qida.clm.core.network.request;

import java.util.HashMap;
import java.util.Map;

import com.qida.clm.core.cache.Cache;
import com.qida.clm.core.network.request.IRequest;
import com.qida.clm.core.network.response.IResponse;
import com.qida.clm.core.utils.DeviceUtils;
import com.qida.clm.core.network.NetworkHelp;

import android.content.Context;

public class BasicRequest<T> implements IRequest<T> {
	private final static String HEAD_UA = "android-clm-2.1.0-14";

	private String mUrl;
    private Map<String,String> mHeadersMap;
    private Map<String,Object> mParamsMap;
    private IResponse<T> mResponse;
    private boolean mShouldCache;
    private Object mTag;
    private final int mMethod;
    private Cache mCache;

    public BasicRequest(String url){
    	this(Method.POST,url);
    }

    public BasicRequest(int method,String url){
        mMethod = method;
        mUrl = url;
    }

    public void addHeaders(Map<String,String> headers){
    	ensureHeaderMap();
        if(headers != null && !headers.isEmpty()){
            mHeadersMap.putAll(headers);
        }
    }

    public Map<String, String> getHeaders(){
    	ensureHeaderMap();
    	mHeadersMap.put("User-Agent", HEAD_UA);
    	Context context = NetworkHelp.getInstance().getContext();
    	mHeadersMap.put("driver-id", DeviceUtils.getDeviceId(context));
    	return mHeadersMap;
    }

    private void ensureHeaderMap(){
    	if(mHeadersMap == null){
    		mHeadersMap = new HashMap<String,String>();
    	}
    }

    private void ensureParamsMap(){
        if(mParamsMap == null){
            mParamsMap = new HashMap<>();
        }
    }

    public void setTag(Object tag){
    	this.mTag = tag;
    }

    public Object getTag(){
    	return mTag;
    }

    public void setShouldCache(boolean shouldCache){
    	this.mShouldCache = shouldCache;
    }

    public boolean getShouldCache(){
    	return mShouldCache;
    }

    @Override
    public Cache<?, T> getCache() {
        return mCache;
    }

    @Override
    public void setCache(Cache cache) {
        this.mCache = cache;
    }

    /**
     * The call method add request params to http body
     * @param params
     */
    public void setParams(Map<String, Object> params){
        if (mParamsMap != null){
            if(params != null && params.size() > 0){
                mParamsMap.putAll(params);
            }
        } else {
            this.mParamsMap = params;
        }
    }

    public void addParams(String key,Object value){
        ensureParamsMap();
        mParamsMap.put(key,value);
    }

	public Map<String, ?> getParams(){
		return mParamsMap;
	}

    public String getUrl(){
		return mUrl;
	}

	public final IResponse<T> getResponse(){
		return mResponse;
	}

    @Override
    public int getMethod() {
        return mMethod;
    }

    @Override
    public void setResponse(IResponse<T> response) {
        this.mResponse = response;
        response.setRequest(this);
    }

    @Override
    public void onBeforeFire() {
        IResponse.ResponseCallback callback = mResponse.getCallBack();
        if(callback != null) callback.onBeforeFire();
    }
}